#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

# 检测是否是root用户
if [ $(id -u) != "0" ]; then
    echo "Error: You must be root to run this script, please use root to install ltnmp"
    exit 1
fi

clear

echo "-------------------------------------------------------------------------"
echo ""
echo "     ltnmp Controll Panel"
echo ""
echo "     By:Andy http://www.moqifei.com"
echo ""
echo "-------------------------------------------------------------------------"

PHPFPMPIDFILE="/usr/local/php/var/run/php-fpm.pid"

arg1=$1
arg2=$2

ltnmp_start() {
    echo "Starting LTNMP..."
    /etc/init.d/nginx start
    if [ -s /etc/init.d/mysql ]; then
        /etc/init.d/mysql start
    fi
    if [ -s /etc/init.d/mariadb ]; then
        /etc/init.d/mariadb start
    fi
    /etc/init.d/php-fpm start
}

ltnmp_stop() {
    echo "Stoping LTNMP..."
    /etc/init.d/nginx stop
    if [ -s /etc/init.d/mysql ]; then
        /etc/init.d/mysql stop
    fi
    if [ -s /etc/init.d/mariadb ]; then
        /etc/init.d/mariadb stop
    fi
    /etc/init.d/php-fpm stop
}

ltnmp_reload() {
    echo "Reload LTNMP..."
    /etc/init.d/nginx reload
    if [ -s /etc/init.d/mysql ]; then
        /etc/init.d/mysql reload
    fi
    if [ -s /etc/init.d/mariadb ]; then
        /etc/init.d/mariadb reload
    fi
    /etc/init.d/php-fpm reload
}

ltnmp_kill() {
    echo "Kill tengine/nginx,php-fpm,mariadb/mysql process..."
    killall nginx
    killall mysqld
    killall php-fpm
    killall php-cgi
    echo "done."
}

ltnmp_status() {
    /etc/init.d/nginx status
    # if [ -f $PHPFPMPIDFILE ]; then
    #     echo "php-fpm is runing!"
    # else
    #     echo "php-fpm is stop!"
    # fi
    /etc/init.d/php-fpm status
    if [ -s /etc/init.d/mysql ]; then
        /etc/init.d/mysql status
    fi
    if [ -s /etc/init.d/mariadb ]; then
        /etc/init.d/mariadb status
    fi
}

function_vhost() {
    case "$1" in
    [aA][dD][dD])
        vhost_add
    ;;
    [lL][iI][sS][tT])
        vhost_list
    ;;
    [dD][eE][lL])
        vhost_del
    ;;
    [eE][xX][iI][tT])
        exit 1
    ;;
    *)
        echo "Usage: ltnmp vhost {add|list|del}"
        exit 1
    ;;
esac
}

function_mysql() {
    case "$1" in
    [aA][dD][dD])
        Add_Database_Menu
        Add_Database
    ;;
    [lL][iI][sS][tT])
        List_Database
    ;;
    [dD][eE][lL])
        Del_Database
    ;;
    [eE][xX][iI][tT])
        exit 1
    ;;
    *)
        echo "Usage: lnmp mysql {add|list|del}"
        exit 1
    ;;
esac
}

function_ftp() {
    case "$1" in
    [aA][dD][dD])
        Add_Ftp_Menu
        Add_Ftp
    ;;
    [lL][iI][sS][tT])
        List_Ftp
    ;;
    [dD][eE][lL])
        Del_Ftp
    ;;
    [eE][xX][iI][tT])
        exit 1
    ;;
    *)
        echo "Usage: ltnmp ftp {add|list|del}"
        exit 1
    ;;
esac
}

vhost_add() {
    domain=""
    read -p "Please input domain(example: www.moqifei.com): " domain
    if [ "${domain}" = "" ]; then
        echo "The domain can't be empty."
        exit 1
    fi
    if [ ! -f "/usr/local/nginx/conf/vhost/${domain}.conf" ]; then
        echo "======================================"
        echo " Your domain is: ${domain}"
        echo "======================================"
    else
        echo "==============================="
        echo "${domain} is exist!"
        echo "==============================="
    fi

    read -p "add more domain? (y/n) " add_more_domainame

    if [ "${add_more_domainame}" == "y" ]; then

        read -p "Enter domain name(example: moqifei.com  *.moqifei.com): " moredomain
        echo "domain list: ${moredomain}"
        moredomainame=" ${moredomain}"
    fi

    vhostdir="/home/www/${domain}"
    echo "Please enter the directory for the domain: $domain"
    read -p "(default: /home/www/${domain}): " vhostdir
    if [ "${vhostdir}" = "" ]; then
        vhostdir="/home/www/${domain}"
    fi
    echo "Virtual Host Directory: ${vhostdir}"

    echo "==========================="
    echo "Allow Rewrite rule? (y/n)"
    echo "==========================="
    read allow_rewrite

    if [ "${allow_rewrite}" == "n" ]; then
        rewrite="none"
    else
        rewrite="other"
        echo "Please enter the rewrite of programme: "
        echo "agile,wordpress,discuz,typecho,sablog,dabr rewrite was exist."
        read -p "(default: other):" rewrite
        if [ "${rewrite}" = "" ]; then
            rewrite="other"
        fi
    fi
    echo "==========================="
    echo "rewrite=${rewrite}"
    echo "==========================="

    echo "==========================="
    echo "Allow access_log? (y/n)"
    echo "==========================="
    read access_log

    if [ "${access_log}" == "n" ]; then
        al="access_log off;"
    else
        read -p "Enter access log name(default:${domain}.log): " al_name
        if [ "${al_name}" = "" ]; then
            al_name="${domain}"
        fi
        al="access_log  /home/www/wwwlogs/${al_name}.log  access;"

    echo "You access log filename: ${al_name}.log"
    fi

    ## 数据库
    # echo "======================================================"
    # echo "Create database and MySQL user with same name (y/n)"
    # echo "======================================================"
    # read create_database

    # if [ "${create_database}" == "y" ]; then
    #     verify_mysql_password
    #     Add_Database_Menu
    # fi

    ## ftp账号
    # if [ -f /usr/local/pureftpd/sbin/pure-config.pl ]; then
    #     echo "======================================================"
    #     echo "Create ftp account (y/n)"
    #     echo "======================================================"
    #     read create_ftp

    #     if [ "${create_ftp}" == "y" ]; then
    #         if [ "${create_database}" != "y" ]; then
    #             verify_mysql_password
    #         fi
    #         Add_Ftp_Menu
    #     fi
    # fi

    echo -e "\nPress any key to start create virtul host..."
    OLDCONFIG=`stty -g`
    stty -icanon -echo min 1 time 0
    dd count=1 2>/dev/null
    stty ${OLDCONFIG}

    echo "Create Virtul Host directory......"
    mkdir -p ${vhostdir}
    if [ "${access_log}" == "n" ]; then
        touch /home/www/wwwlogs/${al_name}.log
    fi
    echo "set permissions of Virtual Host directory......"
    chmod -R 755 ${vhostdir}
    chown -R www:www ${vhostdir}

    if [ ! -f /usr/local/nginx/conf/${rewrite}.conf ]; then
        echo "Create Virtul Host ReWrite file......"
        touch /usr/local/nginx/conf/${rewrite}.conf
        echo "Create rewirte file successful,You can add rewrite rule into /usr/local/nginx/conf/${rewrite}.conf."
    else
        echo "You select the exist rewrite rule:/usr/local/nginx/conf/${rewrite}.conf"
    fi

    cat >/usr/local/nginx/conf/vhost/${domain}.conf<<EOF
server
{
    listen 80;
    #listen [::]:80;
    server_name ${domain} ${moredomainame};
    index index.html index.htm index.php default.html default.htm default.php;
    root  ${vhostdir};

    include ${rewrite}.conf;
    #error_page   404   /404.html;

    location ~ [^/]\.php(/|$)
    {
        try_files \$uri =404;
        fastcgi_index index.php;
        fastcgi_pass  unix:/dev/shm/php-cgi.sock;
        include       fastcgi_params;
        fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
        fastcgi_param SCRIPT_NAME     \$fastcgi_script_name;
    }

    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
        expires      30d;
    }

    location ~ .*\.(js|css)?$
    {
        expires      12h;
    }

    ${al}

}
EOF

#     cat >${vhostdir}/.user.ini<<EOF
# open_basedir=${vhostdir}:/tmp/:/proc/
# EOF
#     chmod 644 ${vhostdir}/.user.ini
#     chattr +i ${vhostdir}/.user.ini

    echo "Restart php"
    /etc/init.d/php-fpm restart

    echo "Test Nginx configure file......"
    /usr/local/nginx/sbin/nginx -t
    echo ""
    echo "Restart Nginx......"
    /usr/local/nginx/sbin/nginx -s reload

    ## 添加数据库账号
    # if [ "${create_database}" == "y" ]; then
    #     Add_Database
    # fi

    ## 添加FTP账号
    # if [ "${create_ftp}" == "y" ]; then
    #     Add_Ftp
    # fi


    echo "================================================"
    echo "Virtualhost infomation:"
    echo "Your domain: ${domain}"
    echo "Home Directory: ${vhostdir}"
    echo "Rewrite: ${rewrite}"
    if [ "${access_log}" = "n" ]; then
        echo "Enable log: no"
    else
        echo "Enable log: yes"
    fi

    ## 添加数据库账号
    # if [ "${create_database}" = "y" ]; then
    #     echo "Database username: ${database_name}"
    #     echo "Database userpassword: ${mysql_password}"
    #     echo "Database Name: ${database_name}"
    # else
    #     echo "Create database: no"
    # fi

    ## 添加FTP账号
    # if [ "${create_ftp}" = "y" ]; then
    #     echo "FTP account name: ${ftp_account_name}"
    #     echo "FTP account password: ${ftp_account_password}"
    # else
    #     echo "Create ftp account: no"
    # fi
    echo "================================================"
}

vhost_list() {
    echo "Nginx Virtualhost list:"
    ls /usr/local/nginx/conf/vhost/ | grep ".conf$" | sed 's/.conf//g'
}

vhost_del() {
    echo "======================================="
    echo "Current Virtualhost:"
    vhost_list
    echo "======================================="
    domain=""
    read -p "Please enter domain you want to delete: " domain
    if [ "${domain}" = "" ]; then
        echo "No enter,domain name can't be empty."
        exit 1
    fi
    if [ ! -f "/usr/local/nginx/conf/vhost/${domain}.conf" ]; then
        echo "=========================================="
        echo "Domain: ${domain} was not exist!"
        echo "=========================================="
        exit 1
    else
        rm -f /usr/local/nginx/conf/vhost/${domain}.conf
        echo "========================================================"
        echo "Domain: ${domain} has been deleted."
        echo "Website files will not be deleted for security reasons."
        echo "You need to manually delete the website files."
        echo "========================================================"
    fi
}

check_db() {
    if [[ -s /usr/local/mariadb/bin/mysql && -s /usr/local/mariadb/bin/mysqld_safe && -s /etc/my.cnf ]]; then
        mysql_bin="/usr/local/mariadb/bin/mysql"
    else
        mysql_bin="/usr/local/mysql/bin/mysql"
    fi
}

verify_mysql_password() {
    read -p "verify your current MySQL root password: " mysql_root_password
    ${mysql_bin} -uroot -p${mysql_root_password} -e "quit"
    if [ $? -eq 0 ]; then
        echo "MySQL root password correct."
    else
        echo "MySQL root password incorrect!Please check!"
        verify_mysql_password
    fi
    if [ "${mysql_root_password}" = "" ]; then
        verify_mysql_password
    fi
}

Enter_Database_Name() {
    read -p "Enter database name: " database_name
    if [ "${database_name}" = "" ]; then
        echo "Database Name can't be empty!"
        exit 1
    fi
}

Add_Database_Menu() {
    Enter_Database_Name
    echo "Your will create a database and MySQL user with same name: ${database_name}"
    read -p "Please enter password for mysql user ${database_name}: " mysql_password
    echo "Your password: ${mysql_password} "
}

Add_Database() {
    ${mysql_bin} -uroot -p${mysql_root_password} -e "CREATE USER '${database_name}'@'127.0.0.1' IDENTIFIED BY '${mysql_password}'"
    ${mysql_bin} -uroot -p${mysql_root_password} -e "CREATE USER '${database_name}'@'localhost' IDENTIFIED BY '${mysql_password}'"
    [ $? -eq 0 ] && echo "User ${database_name} create Sucessfully." || echo "User ${database_name} already exists!"
    ${mysql_bin} -uroot -p${mysql_root_password} -e "GRANT USAGE ON *.* TO '${database_name}'@'127.0.0.1' IDENTIFIED BY '${mysql_password}'"
    ${mysql_bin} -uroot -p${mysql_root_password} -e "GRANT USAGE ON *.* TO '${database_name}'@'localhost' IDENTIFIED BY '${mysql_password}'"
    ${mysql_bin} -uroot -p${mysql_root_password} -e "CREATE DATABASE ${database_name}"
    [ $? -eq 0 ] && echo "Database: ${database_name} create Sucessfully." || echo "Database: ${database_name} already exists!"
    ${mysql_bin} -uroot -p${mysql_root_password} -e "GRANT ALL PRIVILEGES ON ${database_name}.* TO '${database_name}'@'127.0.0.1';"
    ${mysql_bin} -uroot -p${mysql_root_password} -e "GRANT ALL PRIVILEGES ON ${database_name}.* TO '${database_name}'@'localhost';"
    [ $? -eq 0 ] && echo "GRANT ALL PRIVILEGES ON ${database_name} Sucessfully." || echo "GRANT ALL PRIVILEGES ON ${database_name} failed!"
    ${mysql_bin} -uroot -p${mysql_root_password} -e "FLUSH PRIVILEGES"
    [ $? -eq 0 ] && echo "FLUSH PRIVILEGES Sucessfully." || echo "FLUSH PRIVILEGES failed!"
}

List_Database() {
    ${mysql_bin} -uroot -p${mysql_root_password} -e "SHOW DATABASES;"
    [ $? -eq 0 ] && echo "List all databases Sucessfully." || echo "List all databases failed!"
}

Del_Database() {
    List_Database
    Enter_Database_Name
    if [[ "${database_name}" = "information_schema" || "${database_name}" = "mysql" || "${database_name}" = "performance_schema" ]]; then
        echo "MySQL System Database can't be delete!"
        exit 1
    fi
    echo "Your will delete database and MySQL user with same name: ${database_name}"
    echo "Sleep 10s,Press ctrl+c to cancel..."
    sleep 10
    ${mysql_bin} -uroot -p${mysql_root_password} -e "DROP USER '${database_name}'@'127.0.0.1'"
    ${mysql_bin} -uroot -p${mysql_root_password} -e "DROP USER '${database_name}'@'localhost'"
    ${mysql_bin} -uroot -p${mysql_root_password} -e "DROP DATABASE ${database_name}"
    [ $? -eq 0 ] && echo "Database: ${database_name} deleted Sucessfully." || echo "Database: ${database_name} not exist!"
    ${mysql_bin} -uroot -p${mysql_root_password} -e "FLUSH PRIVILEGES"
    [ $? -eq 0 ] && echo "FLUSH PRIVILEGES Sucessfully." || echo "FLUSH PRIVILEGES failed!"
}

Enter_Ftp_Name() {
    read -p "Enter ftp account name: " ftp_account_name
    if [ "${ftp_account_name}" = "" ]; then
        echo "FTP account name can't be empty!"
        exit 1
    fi
}

Add_Ftp_Menu() {
    Enter_Ftp_Name
    read -p "Enter password for ftp account ${ftp_account_name}: " ftp_account_password
    if [ "${ftp_account_password}" = "" ]; then
        echo "FTP password can't be empty!"
        exit 1
    fi
    if [ "${vhostdir}" = "" ]; then
        read -p "Enter directory for ftp account ${ftp_account_name}: " vhostdir
        if [ "${vhostdir}" = "" ]; then
            echo "Directory can't be empty!"
            exit 1
        fi
    fi
}

Check_Pureftpd() {
    if [ ! -f /usr/local/pureftpd/sbin/pure-config.pl ]; then
        echo "Pureftpd was not installed!"
        exit 1
    fi
}

Add_Ftp() {
    www_uid=`id -u www`
    www_gid=`id -g www`
    ${mysql_bin} -uroot -p${mysql_root_password} -e "INSERT INTO ftpusers.users VALUES ('${ftp_account_name}', MD5('${ftp_account_password}'), ${www_uid}, ${www_gid}, '${vhostdir}', 0, 0, 0, 0, '*', '${domain}', '1', 0, 0)"
    [ $? -eq 0 ] && echo "Created FTP User: ${ftp_account_name} Sucessfully." || echo "FTP User: ${ftp_account_name} already exists!"
}

List_Ftp() {
    ${mysql_bin} -uroot -p${mysql_root_password} -e "SELECT User,Dir FROM ftpusers.users users ORDER BY User ASC"
    [ $? -eq 0 ] && echo "List FTP User Sucessfully." || echo "Read database failed."
}

Del_Ftp() {
    List_Ftp
    Enter_Ftp_Name
    echo "Your will delete ftp user ${ftp_account_name}"
    echo "Sleep 10s,Press ctrl+c to cancel..."
    sleep 10
    ${mysql_bin} -uroot -p${mysql_root_password} -e "DELETE FROM ftpusers.users WHERE User='${ftp_account_name}'"
    [ $? -eq 0 ] && echo "FTP User: ${ftp_account_name} deleted Sucessfully." || echo "FTP User: ${ftp_account_name} not exists!"
}

# check_db

case "${arg1}" in
    start)
        ltnmp_start
        ;;
    stop)
        ltnmp_stop
        ;;
    restart)
        ltnmp_stop
        ltnmp_start
        ;;
    reload)
        ltnmp_reload
        ;;
    kill)
        ltnmp_kill
        ;;
    status)
        ltnmp_status
        ;;
    nginx)
        /etc/init.d/nginx ${arg2}
        ;;
    mysql)
        /etc/init.d/mysql ${arg2}
        ;;
    # mariadb)
    #     /etc/init.d/mariadb ${arg2}
    #     ;;
    php)
        /etc/init.d/php-fpm ${arg2}
        ;;
    # pureftpd)
    #     /etc/init.d/pureftpd ${arg2}
    #     ;;
    # httpd)
    #     /etc/init.d/httpd ${arg2}
    #     ;;
    vhost)
        function_vhost ${arg2}
        ;;
    # database)
    #     verify_mysql_password
    #     function_mysql ${arg2}
    #     ;;
    # ftp)
    #     Check_Pureftpd
    #     verify_mysql_password
    #     function_ftp ${arg2}
    #     ;;
    *)
        echo "Usage: ltnmp {start|stop|reload|restart|kill|status}"
        # echo "Usage: ltnmp {nginx|mysql|php-fpm|pureftpd} {start|stop|reload|restart|kill|status}"
        echo "Usage: ltnmp {nginx|mysql|php} {start|stop|reload|restart|kill|status}"
        echo "Usage: ltnmp vhost {add|list|del}"
        # echo "Usage: ltnmp database {add|list|del}"
        # echo "Usage: ltnmp ftp {add|list|del}"
esac
exit